// pages/center/center.js
const myRequest=require("../../utils/myRequest.js")
const eventBus = require('../../utils/eventbus.js');
Page({
  data: {
    logined:false,
    login_name:"",
    password:"",
    username:"",
    amount:""
  },

  onLoad: function (options) {
    let self=this
    function getAmount(){
      myRequest.request({
        url: 'http://localhost:3000/api/userAmount',
        method: 'GET',
        dataType: 'json',
        success: function (res) {
          self.setData({
            amount: res.data.amount
          })
        },
      })
    }
    getAmount()
    eventBus.on("refreshAmount", getAmount)
     this.setData({
       logined: wx.getStorageSync("token")?true:false,
       username: wx.getStorageSync("username")
     })
  },
  changeLoginName(e){
   this.setData({
     login_name: e.detail.value
   })
  },
  changePassword(e){
    this.setData({
      password: e.detail.value
    })
  },
  login(){
    let self=this
    wx.request({
      url: 'http://localhost:3000/api/login',
      data: {
        username:this.data.login_name,
        passworld: this.data.password
      },
      method: 'POST',
      dataType: 'json',
      success: function(res) {
        console.log(res)
        if(res.data.status===10000){
          wx.setStorageSync("token", res.data.data.token)
          wx.setStorageSync("id", res.data.data.user.id)
          wx.setStorageSync("username", res.data.data.user.name)
          self.setData({
            username: res.data.data.user.name,
            logined:true
          })
        }
      }
    })
  },
  logout(){
    wx.removeStorageSync("token")
    wx.removeStorageSync("username")
    wx.removeStorageSync("id")
    this.setData({
      logined: false,
      login_name: "",
      password: "",
      username: ""
    })
  },
  getGender() {
    let id = wx.getStorageSync("id")
    myRequest.request({
      url: 'http://localhost:3000/api/getgender?id=' + id,
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        console.log(res)
      }
    })
  }
  // getGender(){
  //   let id=wx.getStorageSync("id")
  //   wx.request({
  //     url: 'http://localhost:3000/api/getgender?id='+id,
  //     header: {
  //       'cookie':"token="+wx.getStorageSync("token")
  //     },
  //     method: 'GET',
  //     dataType: 'json',
  //     success: function(res) {
  //        console.log(res)
  //     }
  //   })
  // }
})